package repository

import (
	"github.com/gin-gonic/gin"
	"yak.admin/src/entity"
	"yak.admin/src/repository"
	"yak.admin/src/service"
	"yak.admin/src/util"
)

type AdminRepository struct {
	Page      int `form:"page"`
	Pagesize  int `form:"per-page"`
	Id        int `form:"id"`
	Group int
	Type int
	repository.RegionRepository
}

func (repository *AdminRepository) FindSuperAll(c *gin.Context) ([]map[string]interface{}, int64) {
	if err := util.DefaultGetValidParams(c, repository); err != nil {
		return []map[string]interface{}{}, 0
	}

	reader := new(service.ReaderQuery).CreateModelQuery(&entity.Admin{}).
		WithEQ("group", entity.AdminSuper).
		WithPage(repository.Page).WithPageSize(repository.Pagesize)

	var admins []*entity.Admin
	reader.Reader(&admins)
	types := entity.TypeLabels

	res := reader.Datalist(admins, len(admins), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.Admin)
		return map[string]interface{}{
			"_serial": reader.AsSerial(index),
			"id": m.Id,
			"username": m.Username,
			"password": m.Password,
			"nickname": m.Nickname,
			"last_login_time": reader.AsDatetime(m.LastLoginTime),
			"type": types[m.Type],
		}
	})

	return res, reader.Count()
}


func (repository *AdminRepository) FindNormalAll(c *gin.Context, t int) ([]map[string]interface{}, int64) {
	if err := util.DefaultGetValidParams(c, repository); err != nil {
		return []map[string]interface{}{}, 0
	}

	reader := new(service.ReaderQuery).CreateModelQuery(&entity.Admin{}).
		WithEQ("group", entity.AdminNormal).
		WithEQ("type", t).
		WithPage(repository.Page).WithPageSize(repository.Pagesize)

	var admins []*entity.Admin
	reader.Reader(&admins)

	res := reader.Datalist(admins, len(admins), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.Admin)
		return map[string]interface{}{
			"_serial": reader.AsSerial(index),
			"id": m.Id,
			"username": m.Username,
			"password": m.Password,
			"nickname": m.Nickname,
			"last_login_time": reader.AsDatetime(m.LastLoginTime),
			"address": m.Address,
		}
	})

	return res, reader.Count()
}